//
//  RecommedHelper.m
//  JZMCFM
//
//  Created by zhangxin.com on 15/11/2.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "RecommedHelper.h"
#import "RecommedTitleModel.h"
@implementation RecommedHelper
static RecommedHelper *recoment = nil;
+(RecommedHelper*)shareInstance
{
    static RecommedHelper *recoment = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        recoment = [[RecommedHelper alloc]init];
        recoment.RecommedDataArray = [NSMutableArray array];
        recoment.editorRecommendAlbumsArray = [NSMutableArray array];
//        recoment.hotRecommendsArray = [NSMutableArray array];
        recoment.specialColumnArray = [NSMutableArray array];
        recoment.entrancesArray = [NSMutableArray array];
        recoment.discoveryColumnsArray = [NSMutableArray array];
        recoment.aaaa = [NSMutableArray array];
    });
    return recoment;
}

- (void)downLoadFinished:(void(^)())result
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [SVProgressHUD showWithStatus:@"亲亲亲,正在加载,做这稍等会儿" maskType:SVProgressHUDMaskTypeBlack];
    
        //获取数据
        NSURL *url = [NSURL URLWithString:Urlcommend];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [NSURLConnection  sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            //        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
            NSDictionary * commendDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             
            
            
            NSMutableArray *listArray = commendDic[@"focusImages"][@"list"];
            
            for (NSDictionary *listDic in listArray) {
                RecommedModel *model = [[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:listDic];
                [self.RecommedDataArray addObject:model];
                
            }
            
            NSDictionary *editorDic = commendDic[@"editorRecommendAlbums"];
            NSMutableArray *array = editorDic[@"list"];
            for (NSDictionary *listDic in array) {
                RecommedModel *model = [[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:listDic];
                [self.editorRecommendAlbumsArray addObject:model];
                
            }
            
            NSDictionary *hotdic = commendDic[@"hotRecommends"];
            NSMutableArray *hotlistArray = hotdic[@"list"];
            
            for (NSDictionary *hotlistDic in hotlistArray) {
                NSMutableArray *a = hotlistDic[@"list"];
                self.hotRecommendsArray = [NSMutableArray array];
                    for (NSMutableDictionary *dchot in a) {
                        RecommedModel *model  = [[RecommedModel alloc]init];
                        [model setValuesForKeysWithDictionary:dchot];
                        [self.hotRecommendsArray addObject:model];
                        NSLog(@"%@",model.trackTitle);
                    }
                [self.aaaa addObject:self.hotRecommendsArray];
                }
            
            NSDictionary *specialDic= commendDic[@"specialColumn"];
            NSMutableArray *sArray = specialDic[@"list"];
            for (NSDictionary *slistdic in sArray) {
                RecommedModel *model = [[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:slistdic];
                [self.specialColumnArray addObject:model];
            }
            
            NSDictionary *entrandict = commendDic[@"entrances"];
            NSMutableArray *arrayentran = entrandict[@"list"];
            for (NSDictionary *enlistdic in arrayentran) {
                RecommedModel *model = [[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:enlistdic];
                [self.entrancesArray addObject:model];
            
                
            }
            
            NSDictionary *discoverydic = commendDic[@"discoveryColumns"];
            NSMutableArray *disarray = discoverydic[@"list"];
            for (NSDictionary *dislistdic in disarray) {
                RecommedModel *model = [[RecommedModel alloc]init];
                [model setValuesForKeysWithDictionary:dislistdic];
                [self.discoveryColumnsArray addObject:model];
            }
            
            RecommedModel * model =[[RecommedModel alloc]init];
            // 小编推荐
            [model setValuesForKeysWithDictionary:editorDic];
            [self.titleArray addObject:model.title];
            // 精品听单
            [model setValuesForKeysWithDictionary:specialDic];
            [self.titleArray addObject:model.title];
//            // 发现新奇
//            [model setValuesForKeysWithDictionary:discoverydic];
//            [self.titleArray addObject:model.title];
            // 热门推荐
            for (NSDictionary * dic in hotlistArray) {
                [model setValuesForKeysWithDictionary:dic];
                [self.titleArray addObject:model.title];
               // NSLog(@"%@",model.title);
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                result();
            });
            
            [SVProgressHUD showSuccessWithStatus:@"加载成功!" duration:2.0];
            
            
        }];
        
        
        
    });
}

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

@end
